/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.imageformat.keyframes;

import com.facebook.drawee.drawable.Animatable;
import com.facebook.drawee.drawable.ForwardingDrawable;
import com.facebook.keyframes.KeyframesDrawable;

/**
 * Animation of KeyframesDrawables needs to be explicitly started and stopped.
 * AnimatableKeyframesDrawable wraps a KeyframesDrawables and allows Fresco to automatically start
 * and stop the animated as needed (as long as setAutoPlayAnimations(true) is called on the
 * DraweeController associated with the DraweeView used to display the image.
 */
public class AnimatableKeyframesDrawable extends ForwardingDrawable implements Animatable {

  private final KeyframesDrawable mDrawable;
  private boolean mAnimating;

  public AnimatableKeyframesDrawable(KeyframesDrawable drawable) {
    super(drawable);
    mDrawable = drawable;
  }

  @Override
  public void start() {
    mDrawable.startAnimation();
    mAnimating = true;
  }

  @Override
  public void stop() {
    mDrawable.stopAnimation();
    mAnimating = false;
  }

  @Override
  public boolean isRunning() {
    return mAnimating;
  }
}
